<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace app\home\controller;

use controller\BasicHome;
use think\Db;

/**
 * 药品库
 * Class Drug
 * @package app\home\controller
 * @author pp <124910168@qq.com>
 * @date 2020/12/15 10:41
 */
class Drug extends BasicHome
{
    public function _initialize()
    {
        parent::_initialize();
        $allNums = count($this->drug->getLists(['status' => 1, 'aid' => ['in', _getAIDs(1)]]));
        $itemAreas = $this->areas;
        foreach ($itemAreas as $k => $v) {
            $itemAreas[$k]['num'] = count($this->drug->getLists(['status' => 1, 'aid' => ['in', _getAIDs($v['area_id'])]]));
        }
        // halt($itemAreas);
        $this->assign('itemAreas', $itemAreas);
        $this->assign('allNums', $allNums);
        
        $this->drugCateList = Db::name('Drugcate')->where(['status' => 1, 'is_deleted' => 0])->order('sort asc ')->select();
        $this->assign('drugCateList', $this->drugCateList);
    }

    //首页
    public function index(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did', 'cid'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did', 'cid'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['title|alias|desc|content|character|efficacy'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->drug->getPageLists($map, 'createtime desc', '');
        // halt($list);
        
        //推荐
        $recommend = $this->drug->getLists(['status' => 1]);
        // halt($recommend);

        $data = [
            'recommend' => $recommend,
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
            'kw' => !empty($get['kw']) ? $get['kw'] : '',
            'aid' => !empty($get['aid']) ? $get['aid'] : '',
            'nid' => !empty($get['nid']) ? $get['nid'] : '',
            'did' => !empty($get['did']) ? $get['did'] : '',
            'cid' => !empty($get['cid']) ? $get['cid'] : '',
        ];
        return $this->fetch('', $data);
    }

    //列表
    public function list(){
        if(!input('cid')){
            $this->error('找不到数据');
        }
        $map = $this->map;
        $map['catid'] = input('cid');
        $map1 = $this->map1;
        $map1['catid'] = input('cid');
        $list = $this->article
                ->where($map)
                ->whereOr(function ($q) use($map1) {
                        $q->where($map1);
                    })
                ->order('id desc')
                ->select();
        $catname = $this->category->where(['id' => input('cid')])->value('catname');
        $data = [
            'catname' => $catname,
            'data' => $list,
        ];
        return $this->fetch('', $data);
    }

    //详情
    public function detail(){
        // $this->_ckR();
        if(!input('id')){
            $this->error('找不到数据');
        }
        $info = $this->drug->where(['id' => input('id')])->find();
        Db::name('Drug')->where(['id' => input('id')])->setInc('hits');

        //上一页
        $prev = $this->drug->where(['status' => 1, 'id' => ['lt', input('id')]])->find();
        //下一页
        $next = $this->drug->where(['status' => 1, 'id' => ['gt', input('id')]])->find();
        //推荐
        $recommend = $list = $this->drug->getLists(['status' => 1, 'id' => ['neq', input('id')]]);

        $data = [
            'info' => $info,
            'prev' => $prev,
            'next' => $next,
            'recommend' => $recommend,
        ];
        // halt($recommend);
        return $this->fetch('', $data);
    }

}
